package com.example.FirstBook.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.FirstBook.Bean.FoodBean;
import com.example.myapplication.R;

public class FoodNutritionItemAdapter extends BaseAdapter {

    private List<FoodBean> objects = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;

    public FoodNutritionItemAdapter(Context context,List<FoodBean> objects) {
        this.objects=objects;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.food_nutrition_item, null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder=(ViewHolder)convertView.getTag();
        }
        FoodBean foodBean=objects.get(position);
        holder.footNutritionText.setText(foodBean.getTitle());
        holder.footNutritionImage.setImageResource(foodBean.getImage());
        return convertView;
    }

    protected class ViewHolder {
        private ImageView footNutritionImage;
    private TextView footNutritionText;

        public ViewHolder(View view) {
            footNutritionImage = (ImageView) view.findViewById(R.id.foot_nutrition_image);
            footNutritionText = (TextView) view.findViewById(R.id.foot_nutrition_text);
        }
    }
}
